import static org.junit.Assert.*;

import org.junit.Test;


public class LinkedListDeleteTest {
	
	@Test
	public void testDelete() {
		//instantiate list
		LinkedList l = new LinkedList();
		
		//insert successive values that can be predicted to test against actual values
		l.insert(10);
		l.insert(9);
		l.insert(8);
		l.insert(7);
		l.insert(6);
		l.insert(5);
		l.insert(4);
		l.insert(3);
		l.insert(2);
		l.insert(1);
		l.insert(0);
		
		for(int i = 0; i <= 10; i++){
			//instantiate the first node
			Node test = l.start;
			
			while(test.getData() != i){
				//find the node with the current value
				test = test.getNext();
			}
			//debug statements
			if(test.getData() == i){
				System.out.println("Node " + i + " is " + test.getData());
			}else{
				System.out.println("Couldn't find node " + i);
			}
		}
		
		//try deleting the first node 6 times
		for(int i = 10; i >= 5; i--){
			//instantiate the first node and the next node
			Node temp = l.start;
			Node next = temp.getNext();
			//delete the first node
			l.delete(i);
			//debug statement
			System.out.println("Deleting " + i + ": temp = " + temp.getData() + "; next = " + next.getData() + 
					"; l.start = " + l.start.getData());
			if(i != 0){
				//ensure that the first node is now the next node instead of the previous first node
				assertEquals(l.start.getData(), next.getData());
			}
			else{
				//ensure that, if the last node was the only node, all nodes are now null
				assertNull(next);
			}
		}
		
		//try deleting later nodes, not just the first
		for(int i = 0; i < 5; i++){
			//instantiate the first node
			Node temp = l.start;
			
			//attempt to delete the last node in the list
			l.delete(i);
			//set the temp node to what should replace the deleted node as the last
			while(temp.getData() > (i+1)){
				temp = temp.getNext();
			}
			//debug statement
			System.out.println("Deleting " + i + ": temp = " + temp.getData() + "; next = " + temp.getNext().getData() + 
					"; l.start = " + l.start.getData());
			//ensure that the last node is now gone
			assertNull(temp.getNext());
		}
	}
}
